package com.store.framework.gui.hk;

import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;

import javax.swing.filechooser.FileSystemView;

import com.store.framework.common.FileHandler;

public class FS implements IFS{
	String[] roots;
	
	public static Connection conn;
	static
	{
		String root = System.getProperties().getProperty("user.dir");
		 InputStream is = FS.class.getResourceAsStream("/type");
		 // Properties properties = new Properties();
		 // properties.load(is);
		ArrayList <String> list = FileHandler.getSourcedataList(is);
		for(String str : list){
			typeList.add(str);
		}
	}
	private Connection getConn(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			if(conn!=null&&!conn.isClosed()){
				return conn;
			}
			return conn = DriverManager.getConnection(
					"jdbc:mysql://limali.gotoftp3.com:3306/limali?characterEncoding=utf-8",
					"limali", 
					"limali");
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		try {
			System.out.println("loading......");
			Thread.sleep(2000);
		} catch (InterruptedException e) {
		}
		return getConn();
	}
	public void init(){
		roots = listRoots();
	}
	
	FileSystemView sys = FileSystemView.getFileSystemView();
	private String[] listRoots(){
		File[] roots = File.listRoots();
		ArrayList<String> listroots = new ArrayList<String>();
		for (int i = 0; i < roots.length; i++) {
			File fs = roots[i];
			if(fs.isDirectory()){
				listroots.add(fs.toString());
//				System.out.println(sys.getSystemTypeDescription(fs));
			}
		}
		String[] result = new String[listroots.size()];
		listroots.toArray(result);
		return result;
	}
	
	public void execute(String author) throws SQLException, ClassNotFoundException{
		
	    
		
		for(String root : roots){
			ArrayList<FileEntry> list = readDirectory(new File(root),author);
//			for(FileEntry fe : list){
//				System.out.println(fe.getName());
//			}
			System.out.println(list.size());
		}
		
		
	}
	
	String sql = "insert into sys_files values(?,?,?,?,?,?,?,?,?,?,?)";
	PreparedStatement st;
	/**
	 * 根据磁盘读目录
	 * @param root
	 * @throws SQLException 
	 */
	private ArrayList<FileEntry> readDirectory(File file,String author) throws SQLException{
		
		if(file.isDirectory()){
			File[] files = file.listFiles();
			if(files!=null){
				for(File f : files){
					st = getConn().prepareStatement(sql);
					if(f.isDirectory()){
						readDirectory(f,author);
					}else{
						// file
						String fname = f.getName();
						String type = fname.substring(fname.lastIndexOf(".")+1);
						
						if(!typeList.contains(type.toLowerCase())){
							st = null;
							continue;
						}
						Timestamp time = new Timestamp(new Date().getTime());
						
						FileEntry fe = new FileEntry();
						fe.setType(type);
						fe.setName(fname);
						fe.setCreateTime(time); 
						fe.setUpdateTime(time);
						fe.setPath(f.getAbsolutePath());
						fe.setAuthor(author);
						
//						System.out.println(f.getAbsolutePath());
						{
							st.setString(1, UUID.randomUUID().toString());
							st.setString(2, fe.getName());
							st.setString(3, fe.getCode());
							
							st.setTimestamp(4,  fe.getCreateTime());
							st.setTimestamp(5, fe.getUpdateTime());
							st.setString(6, fe.getDescript());
							
							st.setString(7, fe.getIndex());
							st.setString(8, fe.getPath());
							st.setString(9, fe.getType());
							st.setString(10, fe.getDoc());
							st.setString(11, fe.getAuthor());

							st.execute();
							st.close();
						}
						list.add(fe);
					}
				}
			}
		}else{
			// FILE
			throw new RuntimeException("file is file");
		}
		
		return list;
	}
	
	public void destroy(){
		roots = null;
	}
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		FS fs = new FS();
		fs.init();
		fs.execute("hy");
		fs.destroy();
		
	}
	
}
